package eoeProject.edenOfEast.page.topPage.component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import eoeProject.edenOfEast.bean.EOEAnalysesBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES;

//@AuthorizeInstantiation("ADMIN")
public class TopPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private BookmarkablePageLink<WebPage> linkToTactics = new BookmarkablePageLink<WebPage>(
			"linkToTactics",
			eoeProject.edenOfEast.page.tactics.tacticsTop.Index.class);
	private BookmarkablePageLink<WebPage> linkToAnalyses = new BookmarkablePageLink<WebPage>(
			"linkToAnalyses",
			eoeProject.edenOfEast.page.analyses.analysesTop.Index.class);
	private BookmarkablePageLink<WebPage> linkToArchives = new BookmarkablePageLink<WebPage>(
			"linkToArchives",
			eoeProject.edenOfEast.page.archives.archivesTop.Index.class);

	public final static Integer TITLE_MAX_LENGTH = 14;

	public TopPanel(String id) {
		super(id);
		add(linkToTactics.setOutputMarkupId(true));
		add(linkToAnalyses.setOutputMarkupId(true));
		add(linkToArchives.setOutputMarkupId(true));

		List<EOEAnalysesBean> analysesBeanList = new LinkedList<EOEAnalysesBean>();
		SQLMaker analysesSql = new SQLMaker(DB_EOE_ANALYSES.TABLE_NAME,
				SQLMaker.SQL_TYPE_SELECT);
		analysesSql.addExpEquals(DB_EOE_ANALYSES.STATUS, 1);
		analysesSql.addOrderBy(DB_EOE_ANALYSES.UPDATE_DATE, "desc");
		analysesSql.addLimit(5);
		ResultSet rs = DBConnector.getInstance().get(analysesSql.getSQL());
		try {
			while (rs.next()) {
				EOEAnalysesBean bean = new EOEAnalysesBean();
				bean.setAnalysesId(rs.getInt(DB_EOE_ANALYSES.ANALYSES_ID));
				bean.setTitle(rs.getString(DB_EOE_ANALYSES.TITLE));
				bean.setCreateDate(rs.getTimestamp(DB_EOE_ANALYSES.CREATE_DATE));
				analysesBeanList.add(bean);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println("AnalysesTopListPanelException: "
					+ e.getMessage());
		}

		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd ");
		SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm ");
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");
		int count = 0;
		for (EOEAnalysesBean bean : analysesBeanList) {
			count++;
			PageParameters parameters = new PageParameters();
			parameters.add("analysesId", bean.getAnalysesId());
			BookmarkablePageLink<WebPage> link = new BookmarkablePageLink<WebPage>(
					"linkToAnalysesRes" + count,
					eoeProject.edenOfEast.page.analyses.analysesRes.Index.class,
					parameters);
			String titleValue = bean.getTitle();
			if (titleValue.length() > TITLE_MAX_LENGTH) {
				titleValue = bean.getTitle().substring(0, TITLE_MAX_LENGTH - 1);
				titleValue = titleValue + "…";
			}
			link.add(new Label("linkToAnalysesResLabel" + count, titleValue));
			add(link);
			if (Integer.valueOf(sdf3.format(bean.getCreateDate())) < Integer
					.valueOf(sdf3.format(new Date()))) {
				add(new Label("dateForAnalysesResLabel" + count,
						sdf.format(bean.getCreateDate())));
			} else {
				add(new Label("dateForAnalysesResLabel" + count,
						sdf2.format(bean.getCreateDate())));
			}
		}
		for (int i = count + 1; i <= 5; i++) {
			BookmarkablePageLink<WebPage> link = new BookmarkablePageLink<WebPage>(
					"linkToAnalysesRes" + i,
					eoeProject.edenOfEast.page.topPage.Index.class);
			link.add(new Label("linkToAnalysesResLabel" + i, "---"));
			add(link);
			add(new Label("dateForAnalysesResLabel" + i, "-/-"));
		}
	}
}
